var express = require('express');
var router = express.Router();
//引入连接池模块
const pool = require('../pool');

// 1.获取首页商品列表的信息
//接口地址：http://127.0.0.1:3000/product/list
// 请求方式:get
router.get('/list', (req, res, next) => {
  
  var sql = `select * from wjx_laptop`;
  pool.query(sql, (err, result) => {
    if (err) throw err;
    res.json(result)
  })
});

// 1.1获取搜索商品列表的信息
//接口地址：http://127.0.0.1:3000/product/lists/三文鱼
// 请求方式:get
router.get('/lists/:title', (req, res) => {
  let obj = req.params;
  console.log(obj);
  var sql = `select * from wjx_laptop where title like '%${obj.title}%'`;
  pool.query(sql, (err, result) => {
    if (err) throw err;
    res.json(result)
  })
});


//2.获取商品详情图下部接口
//接口地址：http://127.0.0.1:3000/product/detail/:lid
// 请求方式:get
router.get("/detail/:laptop_id", (req, res, next) => {
  let obj = req.params;
  console.log(obj);
  pool.query("select * from wjx_laptop_pic where laptop_id=?", [obj.laptop_id], (err, result) => {
    if (err) {
      next(err);
      return;
    }
    res.json({
      results: result
    });
  })
})
//3. 购物车
//接口地址：http://127.0.0.1:3000/product/shopcart
// 请求方式:get
router.get('/shopcart', (req, res) => {
  let lid = req.query.lid;
  // console.log(req.query)
  pool.query('select * from  wjx_shopping_cart', (err, result) => {
    if (err) throw err;
    res.send(result)
  })
})

//4.获取商品分类名称
// 请求方式：get
// 路由地址：http://127.0.0.1:3000/product/fenlei
router.get('/fenlei', (req, res) => {
  // let fid=req.query.fid;
  var sql = "select * from wjx_laptop_family";
  pool.query(sql, (err, result) => {
    if (err) throw err;
    res.json(result)
  })
});

//5、 获取商品分类的商品
// 请求方式：get
// 路由地址：http://127.0.0.1:3000/product/classify?
router.get("/classify/1", (req, res) => {
  let fid = req.query.fid;
  let sql = "select * from list inner join wjx_laptop_family on wjx_laptop_family_fid=fid where fid=?";
  pool.query(sql, [fid], (err, result) => {
    if (err) throw err;
    res.send(result)
  })
})
//6.获取商品详情图上部接口
//接口地址：http://127.0.0.1:3000/product/details/:lid
// 请求方式:get
router.get("/details/:lid", (req, res, next) => {
  let obj = req.params;
  console.log()
  pool.query("select * from wjx_laptop where lid=?", [obj.lid], (err, result) => {
    if (err) {
      next(err);
      return;
    }
    res.json({
      results: result
    });
  })
})












module.exports = router;